package view;

//models
import model.AcolyteDeck;
import model.AcolyteCard;
import model.AcolytePlayer;
//network
import network.AcolyteGameServer;
import network.AcolytePlayerClient;
import network.ServerEvent;
//general utility
import java.util.List;
import java.util.LinkedList;
//graphics classes
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.JFileChooser;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.event.MenuListener;
import javax.swing.event.MenuEvent;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.Graphics;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.IOException;

public class GameTable  extends JFrame implements ActionListener
{
  //null if you are connected to someone else
  protected AcolyteGameServer server = null;
  //player client thread even if you are hosting you use this
  protected AcolytePlayerClient localPlayer;
  protected JTextField chatBar;
  protected java.awt.List chatArea;
  
  
  public GameTable()
  {
    localPlayer = new AcolytePlayerClient(this);
    setUpAndShow();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  public void update(Graphics g)
  {
    System.exit(-1);
    super.update(g);
    System.out.println("test");
    List<String> chat = localPlayer.getLog();
    if(chat != null)
    {
      for(int i=chatArea.getItemCount(); i < chat.size(); i++)
      {
        chatArea.add(chat.get(i));
      }
    }
    System.out.println("chat");
  }
  
  //process any mouse or keyboard events
  public void actionPerformed(ActionEvent e)
  {
    //System.out.println(e.paramString());
    //System.out.println(e);
    System.out.println(e.getActionCommand());
    if("_new" == e.getActionCommand())
    {
      newGame();
    }
    else if("_edit" == e.getActionCommand())
    {
      new DeckEditor();
    }
    else if("_host" == e.getActionCommand())
    {
      hostGame();
      //start a game server thread then spawn a player connected to localhost
    }
    else if("_join" == e.getActionCommand())
    {
      joinGame();
      //try and connect to a remote host
    }
    else if("_chat" == e.getActionCommand())
    {
      try
      {
        //TODO in future append broadcast to this
        //leave it alone onw for testing
        localPlayer.message(chatBar.getText());
        chatBar.setText("");
      }
      catch(IOException ex)
      {
        //TODO popup aboutmessage failure
        ex.printStackTrace();
      }
      System.out.println(chatArea);
    }
    else if("_server" == e.getActionCommand())
    {
      ServerEvent se = (ServerEvent) e;
      String chat = se.getArgumentString();
      //chatArea updates with this String
      //System.out.println("CHAT::" + chat);
      chatArea.add(chat);
    }
    else
    {
      System.out.println(e);
      System.out.println("Unknown event occured:");  
    }
  }
  
  //set up the table for a new game
  private void newGame()
  {
  }
  
  //setup the window for when Acolyte is started
  private void setUpAndShow()
  {
    JPanel content = new JPanel(new GridLayout(3, 0));
    content.add(createChat());
    this.setContentPane(content);
    this.setJMenuBar(createMenuBar());
    this.pack();
    this.setVisible(true);
  }
  
  
  private JPanel createChat()
  {
    JPanel chat = new JPanel(new GridLayout(2,0));
    chatBar = new JTextField("send a message to the server", 20);
    chatBar.addActionListener(this);
    chatBar.setActionCommand("_chat");
    
    chatArea = new java.awt.List();
    chat.add(chatArea);
    chat.add(chatBar);
    return chat;
  }
  
  private JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        //, ActionEvent.ALT_MASK));
        menuBar.add(menu);
        //menu.addMenuListener(this);

        //a group of JMenuItems
        menuItem = new JMenuItem("New Game");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("_new");
        menuItem.setMnemonic(KeyEvent.VK_N); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Start a new game.");
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Deck Editor");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("_edit");
        menuItem.setMnemonic(KeyEvent.VK_K); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_K, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Create a new deck.");
        menu.add(menuItem);
        
        menu = new JMenu("Network");
        menu.setMnemonic(KeyEvent.VK_N);
        menuBar.add(menu);
        
        menuItem = new JMenuItem("Host Game Server");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("_host");
        menuItem.setMnemonic(KeyEvent.VK_H);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
               KeyEvent.VK_H, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
               "Host a new game server.");
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Join Game Server");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("_join");
        menuItem.setMnemonic(KeyEvent.VK_J);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
               KeyEvent.VK_J, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
               "Join a game server.");
        menu.add(menuItem);
                                

        return menuBar;
    }
  
  private void hostGame()
  {
    //ask for a listening port
    int port;
    port = Integer.parseInt(JOptionPane.showInputDialog("What port will you listen at?"));
    
    server = new AcolyteGameServer(port);
    //TODO file dialog to get a gametype file
    //tell the server to set the gameType and then create a new game
    
    server.start();
    //spawn a player and connect to the server
    localPlayer.setHost("localhost");
    localPlayer.setPort(port);
    //localPlayer.connect();
    localPlayer.start();
  }
  
  private void joinGame()
  {
    int port;
    String host;
    //ask for a port and a host
    //spawn a player and connect to the player/host
  }
  
  public static void main(String[] args)
  {
    GameTable game = new GameTable();
  }
  
}